//
//  SPNewVersionView.m
//  penzer
//
//  Created by 还带大道 on 2018/11/1.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "SPNewVersionView.h"

@interface SPNewVersionView()
@property (weak, nonatomic) IBOutlet UIView *bgV;
@property (weak, nonatomic) IBOutlet UIView *contentV;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contenV_W;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentV_H;
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UIButton *nowUpdateBtn;
@property (weak, nonatomic) IBOutlet UILabel *contentL;
@property (nonatomic,assign) CGFloat defaultH;

@end

static NSString *_download_url = @"https://cw.pub/5743";


@implementation SPNewVersionView

+ (instancetype)shareHeaderView
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    self.bgV.backgroundColor = RZ_COLOR_ALPHA(0, 0, 0, 0.6);
    self.contentL.numberOfLines = 0;
    self.defaultH = CGRectGetMaxY(self.nowUpdateBtn.frame);
    self.nowUpdateBtn.layer.cornerRadius = self.nowUpdateBtn.frame.size.height*0.5;
    self.contentV.layer.cornerRadius = 10;
    self.contentL.text = @"";

}


- (void)layoutSubviews
{
    [super layoutSubviews];
    self.contenV_W.constant = self.imageV.image.size.width;
    [self.contentL sizeToFit];
    self.contentV_H.constant = self.defaultH + self.contentL.frame.size.height+10;
}


- (void)setContentStr:(NSString *)contentStr
{
    _contentStr = contentStr;
    
    if(contentStr && contentStr.length>0){
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.lineSpacing = 5; //设置行间距
        paragraph.alignment = NSTextAlignmentCenter;
        NSDictionary *attributeDict = @{
                                        NSFontAttributeName:[UIFont systemFontOfSize:14],
                                        NSForegroundColorAttributeName:RZ_COLOR(74, 74, 74),
                                        NSKernAttributeName:@2,//字符间距
                                        NSParagraphStyleAttributeName:paragraph};
        
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:attributeDict];
        self.contentL.attributedText = attributeStr;
    }
   
    [self layoutIfNeeded];
    
}



+ (void)check
{
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&m=bz_activities&ctrl=common&ac=live&ta=app_version
     */
    dict[@"ctrl"] = @"common";
    dict[@"ac"] = @"live";
    dict[@"ta"] = @"ios_app_version";
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
            NSDictionary *dict = res[@"data"];
            NSString * version_new = dict[@"version"];
            NSString * version_now = [self versionNumber];
            NSString *msg = dict[@"des"];
            _download_url = dict[@"download_url"];
            if(![version_now isEqualToString: version_new]){
                [self showWithMsg:msg];
            }
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}




+ (void)showWithMsg:(NSString *)msg
{
    NSString *msgStr = [msg stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    dispatch_async(dispatch_get_main_queue(), ^{
        SPNewVersionView *view = [SPNewVersionView shareHeaderView];
        view.frame = RZ_KEYWINDOW.bounds;
        view.contentStr = msgStr;
        [RZ_KEYWINDOW addSubview:view];
    });
}


+ (NSString *)versionNumber{
    NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
    return[dict objectForKey:@"CFBundleShortVersionString"];
}



- (IBAction)nowUpdate {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_download_url]];
}


- (IBAction)close {
    [self removeFromSuperview];
}

@end
